package com.hst.capacity.domain.service.impl;

import cn.hutool.core.util.StrUtil;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.franchisee.AgentEntity;
import com.hst.capacity.domain.model.request.agent.AgentSaveParam;
import com.hst.capacity.domain.model.request.fleet.FleetSaveParam;
import com.hst.capacity.domain.model.entity.franchisee.FleetEntity;
import com.hst.capacity.domain.service.FleetService;
import com.hst.capacity.infra.convertor.FleetConvertor;
import com.hst.capacity.infra.persistence.jpa.fleet.FleetInforRepository;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FleetServiceImpl implements FleetService {

    @Autowired
    private FleetInforRepository fleetInforRepository;

    /**
     * 保存/更新车队信息
     * @param param
     * @return
     */
    @Override
    public FleetEntity save(FleetSaveParam param) {
        FleetEntity result = null;
        FleetEntity entity = null;
        if (StringUtil.isNotBlank(param.getIdFleet())) {
            entity = fleetInforRepository.findByIdFleet(param.getIdFleet());
            entity.setStatus(param.getStatus());
            if (StringUtil.isNotBlank(param.getIdAgent()))
                entity.setIdAgent(param.getIdAgent());
            entity.setFleetName(param.getFleetName());
            entity.setFleetTel(param.getFleetTel());
            entity.setFleetAdmin(param.getFleetAdmin());
        } else {
            entity = newFleetEntity(param);
            if (StrUtil.isEmpty(param.getIdFleet()))
                entity.setIdFleet(StringUtil.generateUuid());
        }
        result = fleetInforRepository.save(entity);
        return result;
    };

    /**
     * 检查电话是否已经注册
     * @param param
     * @return
     * */
    @Override
    public boolean checkTel(FleetSaveParam param) {
        FleetEntity entity = fleetInforRepository.findByFleetTel(param.getFleetTel());
        boolean result = (entity != null);
        if (StringUtil.isNotBlank(param.getIdFleet()) && result)
            result = !param.getIdFleet().equals(entity.getIdFleet());
        return result;
    };

    private FleetEntity newFleetEntity(FleetSaveParam param){
        FleetEntity fleetEntity = new FleetEntity();
        fleetEntity.setIdFleet(StringUtil.generateUuid());
        fleetEntity.setIdAgent(param.getIdAgent());
        fleetEntity.setIdFranchisee(param.getIdFranchisee());
        fleetEntity.setFleetName(param.getFleetName());
        fleetEntity.setStatus(param.getStatus());
        fleetEntity.setFleetTel(param.getFleetTel());
        fleetEntity.setFleetAdmin(param.getFleetAdmin());
        fleetEntity.setDriverNum(0);
//        fleetInforEntity.setIdUser();
//        fleetInforEntity.setQrCode();
//        fleetInforEntity.setCreateBy();
//        fleetInforEntity.setCreateTime();
//        fleetInforEntity.setUpdateTime();
        return fleetEntity;
    }

}
